﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using UnityEngine;

namespace Nirvana
{

	public class MoFileParser
	{
		public Encoding Encoding { get; set; }
		public MoFileParser()
		{
			this.Encoding = Encoding.UTF8;
		}

		//\u00A0
		private struct MoFileParserSize
		{
			//  \u2009\u204F
			public int length;

			// \u2009\u2050
			public int offset;
		}

		//\u00A0
		private string[] Decode(BinaryReader br, int offset, int length)
		{
			br.BaseStream.Seek((long)offset, SeekOrigin.Begin);
			byte[] bytes = br.ReadBytes(length);
			return this.Encoding.GetString(bytes).Split(new char[1]);
		}

		public Dictionary<string, string[]> GetTranslations(Stream stream)
		{
			if (stream == null || stream.Length < 20L)
			{
				throw new ArgumentException("Stream can not be null of less than 20 bytes long.");
			}
			BinaryReader binaryReader = new BinaryReader(stream);
			uint num = binaryReader.ReadUInt32();
			if (num != 2500072158u)
			{
			}
			uint num2 = binaryReader.ReadUInt32();
			if (num2 >> 16 > 1u)
			{
				throw new Exception(string.Format("Unsupported MO file major revision: {0}.", num2 >> 16));
			}
			int num3 = binaryReader.ReadInt32();
			int num4 = binaryReader.ReadInt32();
			int num5 = binaryReader.ReadInt32();
			MoFileParser.MoFileParserSize[] array = new MoFileParser.MoFileParserSize[num3];
			MoFileParser.MoFileParserSize[] array2 = new MoFileParser.MoFileParserSize[num3];
			binaryReader.BaseStream.Seek((long)num4, SeekOrigin.Begin);
			for (int i = 0; i < num3; i++)
			{
				array[i].length = binaryReader.ReadInt32();
				array[i].offset = binaryReader.ReadInt32();
			}
			binaryReader.BaseStream.Seek((long)num5, SeekOrigin.Begin);
			for (int j = 0; j < num3; j++)
			{
				array2[j].length = binaryReader.ReadInt32();
				array2[j].offset = binaryReader.ReadInt32();
			}
			Dictionary<string, string[]> dictionary = new Dictionary<string, string[]>(num3);
			for (int k = 0; k < num3; k++)
			{
				string[] array3 = this.Decode(binaryReader, array[k].offset, array[k].length);
				string[] value = this.Decode(binaryReader, array2[k].offset, array2[k].length);
				dictionary.Add(array3[0], value);
				Debug.Log("----key -->>>" + array3[0]);
				foreach (var p in value)
				{
					Debug.Log("---value--->>>" + p);
				}


			}
			return dictionary;
		}
	}
}
